package school.controller.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import school.domain.User;
import school.mapper.UserMapper;
import school.support.BaseController;

import javax.servlet.http.HttpSession;

@RequestMapping("admin")
@Controller
public class AdminController extends BaseController {

    @Autowired
    private UserMapper userMapper;

    /**
     * 登录
     * @return
     */
    @GetMapping("login")
    public String login(){
        return "admin/login";
    }

    /**
     * 登录
     *
     * @param username
     * @param password
     * @return
     */
    @PostMapping("login")
    public String login(String username, String password) {
        User user = userMapper.login(username, password);
        if (null == user) return "redirect:login.do";
        session.setAttribute("sessionAdmin", user);
        return "redirect:index.do";
    }

    /**
     * 首页
     * @return
     */
    @RequestMapping("index")
    public String index(){
        return "admin/index";
    }

    /**
     * 退出登录
     *
     * @param session
     * @return
     */
    @GetMapping("logout")
    public String logout(HttpSession session) {
        session.removeAttribute("sessionAdmin");
        return "redirect:index.do";
    }
}
